package com.jjkj.base.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jjkj.base.common.R;
import com.jjkj.base.exception.BusinessException;
import io.jboot.web.controller.JbootController;

/**
 * 业务异常拦截器
 * @author Rlax
 *
 */
public class BusinessExceptionInterceptor implements Interceptor {

	public BusinessExceptionInterceptor() {
	}

	@Override
	public void intercept(Invocation inv) {
		try {
			inv.invoke();
		} catch (BusinessException e) {
			if (inv.getTarget() instanceof JbootController) {
				JbootController controller = inv.getTarget();
				R r = R.error(e.getCode(), e.getMessage());
				controller.renderJson(r);
			}
		}
	}

}
